'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 掠夺战斗记录
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var _ = require('lodash');
var autoIncrement = require('mongoose-auto-increment');
var luckItemSchema = require('./luck/luckItemSchema');

/**
 * 对战战斗记录
 */
var battleRecordSchema = new mongoose.Schema({
    // 用户id
    atk_role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 防守方的角色id
    def_role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 战斗类型: 1掠夺/2复仇
    battle_type: {
        type: Number,
        default: 1
    },

    // 攻击方数据
    atk: {
        // 名字
        name: {
            type: String,
            default: ''
        },

        // 等级
        lv: {
            type: Number,
            default: 1
        },

        // 职业类型
        job_type: {
            type: Number,
            default: 1
        },

        hero: {},
        equip: {},
        soldier_list: []
    },

    // 防守方数据
    def: {
        // 名字
        name: {
            type: String,
            default: ''
        },

        // 总共的金币
        total_gold: {
            type: Number
        },

        // 总共的经验
        total_exp: {
            type: Number
        },

        // 总共的经验
        total_food: {
            type: Number
        },

        // 建筑列表
        build_list: [],

        // 道路列表
        road_list: [Number],

        // 士兵列表
        soldier_list: []
    },

    // 战斗的路块id
    road_id: {
        type: Number,
        default: 0
    },

    // 奖励列表
    reward_list: [luckItemSchema],

    // 重试的次数
    retry_num: {
        type: Number,
        default: 0
    },

    // 星级
    star: {
        type: Number
    },

    // 获取的金币
    earn_gold: {
        type: Number,
        default: 0
    },

    // 获取的粮食
    earn_food: {
        type: Number,
        default: 0
    },

    // 获取的经验
    earn_exp: {
        type: Number,
        default: 0
    },

    // 奖杯
    cup: {
        type: Number,
        default: 0
    },

    // 重生次数
    relive_num: {
        type: Number,
        default: 0
    },

    // 重生消耗的钻石
    relive_diamond_num: {
        type: Number,
        default: 0
    },

    // 创建时间
    create_time: {
        type: Date,
        default: Date.now,
        index: true
    }
});

/**
 * 获取战斗记录的编号
 */
battleRecordSchema.methods.getRecordId = function () {
    return this._id;
};

/**
 * 获取进攻方的角色id
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkRoleId = function () {
    return this.atk_role_id;
};

/**
 * 获取防守方的角色id
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getDefRoleId = function () {
    return this.def_role_id;
};

/**
 * 攻击方的名字
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkName = function () {
    return this.atk.name;
};

/**
 * 攻击方的等级
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkLv = function () {
    return this.atk.lv;
};

/**
 * 进攻方职业类型
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkJobType = function () {
    return this.atk.job_type;
};

/**
 * 攻击方的士兵列表
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkSoldierList = function () {
    var soldier_list = [];
    _.each(this.atk.soldier_list, function (soldier_item) {
        soldier_list.push(soldier_item.soldier_type);
    });
    return soldier_list;
};

/**
 * 攻击方的技能列表
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getAtkSkillList = function () {
    var skill_list = [];
    _.each(this.atk.hero.skill_list, function (skill_item) {
        skill_list.push(skill_item.skill_type);
    });
    return skill_list;
};

/**
 * 获取奖杯数量
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getCup = function () {
    return this.cup;
};

/**
 * 防守方名
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getDefName = function () {
    return this.def.name;
};

/**
 * 获取防守方的建筑列表
 */
battleRecordSchema.methods.getDefBuildList = function () {
    return this.def.build_list;
};

/**
 * 获取防守方的金币总数
 */
battleRecordSchema.methods.getDefTotalGold = function () {
    return this.def.total_gold || 1;
};

battleRecordSchema.methods.getDefTotalFood = function () {
    return this.def.total_food || 1;
};

/**
 * 获取防守方的经验
 */
battleRecordSchema.methods.getDefTotalExp = function () {
    return this.def.total_exp;
};

/**
 * 获取星级
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getStar = function () {
    return this.star || 0;
};

/**
 * 设置星级
 * @param {[type]} star [description]
 */
battleRecordSchema.methods.setStar = function (star) {
    this.star = +star;
};

/**
 * 设置奖杯数
 * @param {[type]} star [description]
 */
battleRecordSchema.methods.setCup = function (cup) {
    this.cup = +cup;
};

/**
 * 设置获取的金币
 * @param {[type]} earn_gold [description]
 */
battleRecordSchema.methods.setEarnGold = function (earn_gold) {
    this.earn_gold = +earn_gold;
};

/**
 * 获取赚到的金币
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getEarnGold = function () {
    return this.earn_gold;
};

/**
 * 设置获取的粮食
 * @param {[type]} earn_gold [description]
 */
battleRecordSchema.methods.setEarnFood = function (earn_food) {
    this.earn_food = +earn_food;
};

/**
 * 获取赚到的粮食
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getEarnFood = function () {
    return this.earn_food;
};

/**
 * 设置获取的经验
 * @param {[type]} earn_exp [description]
 */
battleRecordSchema.methods.setEarnExp = function (earn_exp) {
    this.earn_exp = +earn_exp;
};

/**
 * 获取获得的经验
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getEarnExp = function () {
    return this.earn_exp;
};

/**
 * 获取进度
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getProgress = function () {
    return parseInt(this.earn_gold / this.getDefTotalGold() * 100);
};

/**
 * 获取奖励列表
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getRewardList = function () {
    var reward_list = [];
    _.each(this.reward_list, function (reward_item) {
        if (!!reward_item.getRewardType()) {
            reward_list.push(reward_item);
        }
    });
    return reward_list;
};

/**
 * 重新打开抽奖
 * @return {[type]} [description]
 */
battleRecordSchema.methods.retryLucky = function () {
    this.retry_num++;
};

/**
 * 获取重试的次数
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getRetryNum = function () {
    return this.retry_num;
};

/**
 * 获取成功打开的个数
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getSuccessLuckNum = function () {
    var success_open_num = 0;
    _.each(this.reward_list, function (reward_item) {
        // 判断是否是打开并失败的
        if (!!reward_item.isOpen() && !!reward_item.getRewardType()) {
            success_open_num++;
        }
    });

    return success_open_num;
};

/**
 * 获取打开失败的个数
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getFailLuckNum = function () {
    var fail_open_num = 0;
    _.each(this.reward_list, function (reward_item) {
        // 判断是否是打开并失败的
        if (!!reward_item.isOpen() && !reward_item.getRewardType()) {
            fail_open_num++;
        }
    });

    return fail_open_num;
};

/**
 * 是否三个宝箱都被开启
 */
battleRecordSchema.methods.canLucky = function () {
    var fail_num = this.getFailLuckNum();
    var success_num = this.getSuccessLuckNum();

    // 如果星级小于1星,不能抽奖
    if (!this.star || this.star < 1) {
        return false;
    }

    // 如果成功打开三次,则不能再抽奖了
    if (success_num == 3) {
        return false;
    }

    // 如果失败次数大于重试的次数,不能再抽奖
    return fail_num <= this.retry_num;
};

/**
 * 抽奖操作
 * @param  {[type]} position [description]
 * @return {[type]}          [description]
 */
battleRecordSchema.methods.lucky = function (position) {
    // 如果已经抽取失败
    if (!this.canLucky()) {
        return null;
    }

    // 只能是6个格子
    if (position < 0 || position > 5) {
        console.error('position is error:' + position);
        return null;
    }

    // 判断是否已经抽取过该位置
    var reward_item = this.reward_list[position];
    if (!!reward_item.isOpen()) {
        return null;
    }

    reward_item.setOpen();

    // 获取奖励的数据
    return reward_item;
};

/**
 * 添加重生的次数
 */
battleRecordSchema.methods.addReliveNum = function () {
    this.relive_num++;
};

/**
 * 获取战斗类型
 */
battleRecordSchema.methods.getBattleType = function () {
    return this.battle_type;
};

/**
 * 复活消耗的代币
 * @param {[type]} relive_diamond [description]
 */
battleRecordSchema.methods.addReliveDiamond = function (relive_diamond) {
    this.relive_diamond_num += relive_diamond;
};

/**
 * 获取复活的钻石
 * @param {[type]} relive_diamond [description]
 */
battleRecordSchema.methods.getReliveDiamond = function () {
    return this.relive_diamond_num;
};

/**
 * 获取重生的次数
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getReliveNum = function () {
    return this.relive_num;
};

/**
 * 创建时间
 * @return {[type]} [description]
 */
battleRecordSchema.methods.getCreateTime = function () {
    return moment(this.create_time).unix();
};

/**
 * 判断战斗是否已经结束了
 * @return {Boolean} [description]
 */
battleRecordSchema.methods.isEnd = function () {
    return this.star !== undefined;
};

battleRecordSchema.plugin(autoIncrement.plugin, {
    model: 'BattleRecordSchema',
    startAt: 10000
});

if (!battleRecordSchema.options.toJSON) {
    battleRecordSchema.options.toJSON = {};
}
/* jshint unused:false */
battleRecordSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.create_time;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('BattleRecord', battleRecordSchema);
